package com.zjy.weather.util.location

import android.content.Context
import android.util.Log

import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.zjy.weather.util.SystemUtils

/**
 * Created by wiikii on 16/4/15.
 * Copyright (C) 2014 wiikii. All rights reserved.
 */
class LocationExecutor(context: Context, private val mLocationListener: LocationListener?) {
    private val mContext: Context
    private var mCancelLocation: Boolean = false

    // 高德
    private var mLocationClient: AMapLocationClient? = null
    private val mAMapListener = AMapLocationListener { mapLocation ->
        mapLocation?.let {
            if (mapLocation.errorCode == AMapLocation.LOCATION_SUCCESS) {
                mLocationClient?.let {
                    mLocationListener?.onLocationSuccess(mapLocation)
                    return@AMapLocationListener
                }
            } else {
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e(com.zjy.weather.TAG, "location Error, ErrCode:" + mapLocation.errorCode + ", " +
                        "errInfo:" + mapLocation.errorInfo)
            }
        }
        mLocationListener?.onLocationFailed()
        releaseAMapLocation()
    }

    //声明AMapLocationClientOption对象
    private var mLocationOption: AMapLocationClientOption? = null

    init {
        this.mContext = context.applicationContext
        this.mCancelLocation = false

        initLocationExecutor()
    }

    private fun initLocationExecutor() {
        //初始化定位
        mLocationClient = AMapLocationClient(mContext)
        //设置定位回调监听
        mLocationClient!!.setLocationListener(mAMapListener)
        //初始化AMapLocationClientOption对象
        mLocationOption = AMapLocationClientOption()

        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption!!.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption!!.isOnceLocation = true

        //获取最近3s内精度最高的一次定位结果：
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。
        //如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption!!.isOnceLocationLatest = true

        mLocationOption!!.httpTimeOut = 15000L

        //给定位客户端对象设置定位参数
        mLocationClient!!.setLocationOption(mLocationOption)
    }

    // 开始高德定位
    private fun startAMapLocation() {
        if (!mCancelLocation) {
            try {
                mLocationClient?:let {
                    initLocationExecutor()
                }
                mLocationClient?.startLocation()
            } catch (e: Exception) {
                mLocationListener?.onLocationFailed()
            }
        }
    }


    private fun releaseAMapLocation() {
        mLocationClient?.let {mLocationClient->
            mLocationClient.stopLocation()
            mLocationClient.onDestroy()
        }
    }

    fun cancelLocation() {
        this.mCancelLocation = true
        mLocationClient?.stopLocation()
    }

    //首先使用高德定位,高德定位失败,则继续用IP定位
    fun executeLocation() {
        mCancelLocation = false // 初始化取消标志
        startAMapLocation()
    }
}
